package com.ssm.student.controller;

import com.ssm.student.eo.LoginLogEoPage;
import com.ssm.student.model.LoginLog;
import com.ssm.student.service.LoginLogService;
import com.ssm.student.tool.ResponseMessage;
import com.ssm.student.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对外提供数据服务接口
 */
@Api(tags = "loginlog")
@RestController
@CrossOrigin
public class LoginLogController {
    @Autowired
    private LoginLogService loginLogService;
    @ApiOperation(value = "日志添加方法")
    @RequestMapping(value = "/addLoginLog", method = RequestMethod.GET)
    public ResponseMessage addLoginLog(LoginLog loginLog) {
        int IsAdd = 0;
        //传入数据的判断
        //(1)实体对象不能为空
        if (loginLog != null) {
            //(2)角色名称不能为空
            if (loginLog.getLogdate() != null) {
                //(3)添加角色数据
                IsAdd = loginLogService.AddLoginLog(loginLog);
                if (IsAdd == 1) {
                    return Result.success("保存成功");
                } else if (IsAdd == -1) {
                    return Result.error("日志重复，不能添加");
                } else {
                    return Result.error("保存失败");
                }
            }
        }
        return Result.error("保存失败");
    }

    @ApiOperation(value = "日志删除方法")
    @RequestMapping(value = "/delLoginLog", method = RequestMethod.GET)
    public ResponseMessage delLoginLog(int logid) {
        if (logid != 0) {
            int delInt = 0;
            delInt = loginLogService.DeleteLoginLog(logid);
            if (delInt > 0) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } else {
            return Result.error("删除的主键不能为0请重新输入");
        }
    }

    @ApiOperation(value = "日志修改方法")
    @RequestMapping(value = "/updateLoginLog", method = RequestMethod.GET)
    public ResponseMessage updateLoginLog(LoginLog loginLog) {
        //传入的参数不能为空
        if (loginLog != null) {
            //参数中主键不能为0
            if (loginLog.getLogid() != 0) {
                if (loginLogService.UpdateLoginLog(loginLog) == 1) {
                    //
                    if (loginLog.getLogdate()!=null){
                        if (loginLog.getLoginip().length()<5){
                            return Result.success("更新成功");
                        }
                    }
                }
            }
        }
        else {
            return Result.error("更新传入参数不正确");
        }
        return Result.error("更新传入参数不正确");
    }
    @ApiOperation(value = "根据条件获取信息")
    @RequestMapping(value = "/getAllLoginLog",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    private  ResponseMessage getAllLoginLog(@RequestBody LoginLogEoPage loginLogEoPage){
        Map<String, List> map=new HashMap<>();
        List<LoginLogEoPage> listloginLog=loginLogService.getWhereAll(loginLogEoPage);
        map.put("list",listloginLog);
        return Result.success("200",map);
    }
}
